package core.views;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import core.Runner;
import core.models.items.*;
import javax.swing.JButton;

/**
 * Displays the store.
 * 
 * @author 4c1a
 */
public class InventoryPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTextField moneyTextField = new JTextField();
	private JTextField bulletFieldEdit;
	private JTextField clothingFieldEdit;
	private JTextField foodFieldEdit;
	private JTextField gunFieldEdit;
	private JTextField medicineFieldEdit;
	private JTextField wAxleFieldEdit;
	private JTextField wTongueFieldEdit;
	private JTextField wWheelFieldEdit;
	private JTextField bulletFieldCurrent;
	private JTextField clothingFieldCurrent;
	private JTextField foodFieldCurrent;
	private JTextField gunFieldCurrent;
	private JTextField medicineFieldCurrent;
	private JTextField wTongueFieldCurrent;
	private JTextField wAxleFieldCurrent;
	private JTextField wWheelFieldCurrent;
	private Runner runner;
	private JTextField oxenFieldEdit;
	private JTextField oxenFieldCurrent;
	private JTextField txtNumtoDiscard;
	private JTextField txtCurrentWeight;
	private JTextField weightCaptxt;
	private JButton btnBackToTrail;

	/**
	 * Brings up a screen that allows the user to purchase items such as
	 * bullets, clothes, food, a gun, medicine, wagon axles, wagon tongues,
	 * wagon wheels, and oxen. The screen also displays the player's available
	 * money.
	 * 
	 * @param runner The runner to be used by the panel.
	 */
	public InventoryPanel(Runner runner) {
		this.runner = runner;
		setLayout(null);

		JLabel lblBullet = new JLabel("Bullet");
		lblBullet.setBounds(10, 36, 46, 14);
		add(lblBullet);

		JLabel lblClothing = new JLabel("Clothing");
		lblClothing.setBounds(10, 61, 46, 14);
		add(lblClothing);

		JLabel label = new JLabel("Food");
		label.setBounds(10, 86, 46, 14);
		add(label);

		JLabel label_1 = new JLabel("Gun");
		label_1.setBounds(10, 115, 46, 14);
		add(label_1);

		JLabel label_2 = new JLabel("Medicine");
		label_2.setBounds(10, 140, 63, 14);
		add(label_2);

		JLabel label_3 = new JLabel("WagonAxle");
		label_3.setBounds(10, 165, 99, 14);
		add(label_3);

		JLabel label_4 = new JLabel("WagonTongue");
		label_4.setBounds(10, 190, 99, 14);
		add(label_4);

		JLabel label_5 = new JLabel("WagonWheel");
		label_5.setBounds(10, 215, 99, 14);
		add(label_5);

		bulletFieldEdit = new JTextField();
		bulletFieldEdit.setBounds(100, 31, 40, 17);
		add(bulletFieldEdit);
		bulletFieldEdit.setColumns(10);


		clothingFieldEdit = new JTextField();
		clothingFieldEdit.setBounds(100, 55, 40, 17);
		clothingFieldEdit.setColumns(10);
		add(clothingFieldEdit);


		foodFieldEdit = new JTextField();
		foodFieldEdit.setBounds(100, 80, 40, 17);
		foodFieldEdit.setColumns(10);
		add(foodFieldEdit);

		gunFieldEdit = new JTextField();
		gunFieldEdit.setBounds(100, 109, 40, 17);
		gunFieldEdit.setColumns(10);
		add(gunFieldEdit);


		medicineFieldEdit = new JTextField();
		medicineFieldEdit.setBounds(100, 134, 40, 17);
		medicineFieldEdit.setColumns(10);
		add(medicineFieldEdit);


		wAxleFieldEdit = new JTextField();
		wAxleFieldEdit.setBounds(100, 159, 40, 17);
		wAxleFieldEdit.setColumns(10);
		add(wAxleFieldEdit);


		wTongueFieldEdit = new JTextField();
		wTongueFieldEdit.setBounds(100, 184, 40, 17);
		wTongueFieldEdit.setColumns(10);
		add(wTongueFieldEdit);


		wWheelFieldEdit = new JTextField();
		wWheelFieldEdit.setBounds(100, 209, 40, 17);
		wWheelFieldEdit.setColumns(10);
		add(wWheelFieldEdit);
		
		oxenFieldEdit = new JTextField();
		oxenFieldEdit.setBounds(100, 234, 40, 20);
		add(oxenFieldEdit);
		oxenFieldEdit.setColumns(10);


		JLabel lblAmountOfEach = new JLabel("Amount of each item:");
		lblAmountOfEach.setBounds(172, 11, 141, 14);
		add(lblAmountOfEach);

		bulletFieldCurrent = new JTextField();
		bulletFieldCurrent.setBounds(182, 34, 44, 17);
		bulletFieldCurrent.setEditable(false);
		bulletFieldCurrent.setColumns(10);
		add(bulletFieldCurrent);

		clothingFieldCurrent = new JTextField();
		clothingFieldCurrent.setBounds(182, 59, 44, 17);
		clothingFieldCurrent.setEditable(false);
		clothingFieldCurrent.setColumns(10);
		add(clothingFieldCurrent);

		foodFieldCurrent = new JTextField();
		foodFieldCurrent.setBounds(182, 84, 44, 17);
		foodFieldCurrent.setEditable(false);
		foodFieldCurrent.setColumns(10);
		add(foodFieldCurrent);

		gunFieldCurrent = new JTextField();
		gunFieldCurrent.setBounds(182, 113, 44, 17);
		gunFieldCurrent.setEditable(false);
		gunFieldCurrent.setColumns(10);
		add(gunFieldCurrent);

		medicineFieldCurrent = new JTextField();
		medicineFieldCurrent.setBounds(182, 138, 44, 17);
		medicineFieldCurrent.setEditable(false);
		medicineFieldCurrent.setColumns(10);
		add(medicineFieldCurrent);

		wTongueFieldCurrent = new JTextField();
		wTongueFieldCurrent.setBounds(182, 188, 44, 17);
		wTongueFieldCurrent.setEditable(false);
		wTongueFieldCurrent.setColumns(10);
		add(wTongueFieldCurrent);

		wAxleFieldCurrent = new JTextField();
		wAxleFieldCurrent.setBounds(182, 163, 44, 17);
		wAxleFieldCurrent.setEditable(false);
		wAxleFieldCurrent.setColumns(10);
		add(wAxleFieldCurrent);

		wWheelFieldCurrent = new JTextField();
		wWheelFieldCurrent.setBounds(182, 213, 44, 17);
		wWheelFieldCurrent.setEditable(false);
		wWheelFieldCurrent.setColumns(10);
		add(wWheelFieldCurrent);
		
		JLabel lblOxen = new JLabel("Oxen");
		lblOxen.setBounds(10, 240, 46, 14);
		add(lblOxen);
		
		
		
		oxenFieldCurrent = new JTextField();
		oxenFieldCurrent.setBounds(182, 237, 44, 17);
		oxenFieldCurrent.setEditable(false);
		add(oxenFieldCurrent);
		oxenFieldCurrent.setColumns(10);
		
		txtNumtoDiscard = new JTextField();
		txtNumtoDiscard.setBounds(72, 8, 90, 20);
		txtNumtoDiscard.setBorder(null);
		txtNumtoDiscard.setEditable(false);
		txtNumtoDiscard.setText("NumTo Discard:");
		add(txtNumtoDiscard);
		txtNumtoDiscard.setColumns(10);
		
		JButton btnDiscard = new JButton("Discard");
		btnDiscard.setBounds(10, 266, 89, 23);
		add(btnDiscard);
		
		txtCurrentWeight = new JTextField();
		txtCurrentWeight.setBounds(289, 33, 86, 20);
		txtCurrentWeight.setEditable(false);
		add(txtCurrentWeight);
		txtCurrentWeight.setColumns(10);
		
		weightCaptxt = new JTextField();
		weightCaptxt.setBounds(289, 83, 86, 20);
		weightCaptxt.setEditable(false);
		add(weightCaptxt);
		weightCaptxt.setColumns(10);
		
		
		btnBackToTrail = new JButton("Back to Trail");
		btnBackToTrail.setBounds(120, 265, 106, 23);
		add(btnBackToTrail);
		
		JLabel lblStatus = new JLabel("Status");
		lblStatus.setBounds(289, 115, 46, 14);
		add(lblStatus);
		
		JLabel lblWeightcap = new JLabel("WeightCap");
		lblWeightcap.setBounds(289, 61, 86, 14);
		add(lblWeightcap);
		
		JLabel lblCurrentWeight = new JLabel("Current Weight");
		lblCurrentWeight.setBounds(289, 11, 86, 14);
		add(lblCurrentWeight);
		

		btnDiscard.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				update();
			}
		});
		
		btnBackToTrail.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				StartPanelMain.changeCard("Trail");
			}
		});
	}
	
	public void loadData()
	{
		clearAfterBuy();
		loadItemsQuantity();
		weightCaptxt.setText("" + runner.getGroup().getWagon().getWeightCapacity());
		txtCurrentWeight.setText("" + runner.getGroup().getWagon().getInventory().getTotalWeight());
	}
	/**
	 * Helper method for getting the price of the items.
	 * 
	 */
	private void loadItemsQuantity() {
		List<Item> itemList = runner.getStore().getInventory();
		int money = runner.getGroup().getMoney();
		moneyTextField.setText(Integer.toString(money));

		for (Item item : itemList) {
			String name = item.getName();
			String quantity = Integer.toString(runner.getGroup().getWagon().getInventory().getItemQuantity(name));
			if (name == "Wagon Wheel")
				wWheelFieldCurrent.setText(quantity);
			else if (name == "Wagon Axle")
				wAxleFieldCurrent.setText(quantity);
			else if (name == "Medicine")
				medicineFieldCurrent.setText(quantity);
			else if (name == "Wagon Tongue")
				wTongueFieldCurrent.setText(quantity);
			else if (name == "Guns")
				gunFieldCurrent.setText(quantity);
			else if (name == "Food")
				foodFieldCurrent.setText(quantity);
			else if (name == "Clothing")
				clothingFieldCurrent.setText(quantity);
			else if (name == "Bullets")
				bulletFieldCurrent.setText(quantity);
			else if (name == "Oxen")
				oxenFieldCurrent.setText(quantity);
		}
	}
	/**
	 * Updates the wagon's inventory with recently purchased items.
	 */
	public void update() {	

			List<Item> toDrop = new ArrayList<Item>();
			
			if (!(bulletFieldEdit.getText().equals(""))) {
				try
				{
					if((Integer.parseInt(bulletFieldEdit.getText()))<0)
					{
						bulletFieldEdit.setText("0");
					}
					Bullet b = new Bullet(Integer.parseInt(bulletFieldEdit
							.getText()));
					toDrop.add(b);
				}
				catch(NumberFormatException ex)
				{
					bulletFieldEdit.setText("");
				}

			}
			if (!(clothingFieldEdit.getText().equals(""))) {
				try
				{
					if((Integer.parseInt(clothingFieldEdit.getText()))<0)
					{
						clothingFieldEdit.setText("0");
					}
					Clothing c = new Clothing(Integer.parseInt(clothingFieldEdit
							.getText()));
					toDrop.add(c);
				}
				catch(NumberFormatException ex)
				{
					clothingFieldEdit.setText("");
				}
			}
			if (!(foodFieldEdit.getText().equals(""))) {
				try
				{
					if((Integer.parseInt(foodFieldEdit.getText()))<0)
					{
						foodFieldEdit.setText("0");
					}
					Food f = new Food(Integer.parseInt(foodFieldEdit.getText()));
					toDrop.add(f);
				}
				catch(NumberFormatException ex)
				{
					foodFieldEdit.setText("0");
				}
			}
			if (!(gunFieldEdit.getText().equals(""))) {
				try
				{
					if((Integer.parseInt(gunFieldEdit.getText()))<0)
					{
						gunFieldEdit.setText("0");
					}
					Gun g = new Gun(Integer.parseInt(gunFieldEdit.getText()));
					toDrop.add(g);
				}
				catch(NumberFormatException ex)
				{
					gunFieldEdit.setText("0");
				}
			}
			if (!(medicineFieldEdit.getText().equals(""))) {
				try
				{
					if((Integer.parseInt(medicineFieldEdit.getText()))<0)
					{
						medicineFieldEdit.setText("0");
					}
					Medicine m = new Medicine(Integer.parseInt(medicineFieldEdit.getText()));
					toDrop.add(m);
				}
				catch(NumberFormatException ex)
				{
					medicineFieldEdit.setText("0");
				}
			}
			if (!(wAxleFieldEdit.getText().equals(""))) {
				try
				{
					if((Integer.parseInt(wAxleFieldEdit.getText()))<0)
					{
						wAxleFieldEdit.setText("0");
					}
					WagonAxle wa = new WagonAxle(Integer.parseInt(wAxleFieldEdit.getText()));
					toDrop.add(wa);
				}
				catch(NumberFormatException ex)
				{
					wAxleFieldEdit.setText("0");
				}
			}
			if (!(wTongueFieldEdit.getText().equals(""))) {
				try
				{
					if((Integer.parseInt(wTongueFieldEdit.getText()))<0)
					{
						wTongueFieldEdit.setText("0");
					}
					WagonTongue wt = new WagonTongue(Integer.parseInt(wTongueFieldEdit.getText()));
					toDrop.add(wt);
				}
				catch(NumberFormatException ex)
				{
					wTongueFieldEdit.setText("0");
				}
			}
			if (!(wWheelFieldEdit.getText().equals(""))) {
				try
				{
					if((Integer.parseInt(wWheelFieldEdit.getText()))<0)
					{
						wWheelFieldEdit.setText("0");
					}
					WagonWheel ww = new WagonWheel(Integer.parseInt(wWheelFieldEdit.getText()));
					toDrop.add(ww);
				}
				catch(NumberFormatException ex)
				{
					wWheelFieldEdit.setText("0");
				}
			}
			if (!(oxenFieldEdit.getText().equals(""))) {
				try
				{
					if((Integer.parseInt(oxenFieldEdit.getText()))<0)
					{
						oxenFieldEdit.setText("0");
					}
					Oxen o = new Oxen(Integer.parseInt(oxenFieldEdit.getText()));
					toDrop.add(o);
				}
				catch(NumberFormatException ex)
				{
					oxenFieldEdit.setText("0");
				}
			}
			
			
				runner.getGroup().getWagon().getInventory().remove(toDrop);
				runner.getGroup().getWagon().getInventory().clean();
				loadItemsQuantity();
				weightCaptxt.setText("" + runner.getGroup().getWagon().getWeightCapacity());
				txtCurrentWeight.setText("" + runner.getGroup().getWagon().getInventory().getTotalWeight());
				clearAfterBuy();
				System.out.println(runner.getGroup().getWagon().getInventory());
	}
	/**
	 * Resets all text fields to blank after "Buy" button is pressed.
	 */
	private void clearAfterBuy()
	{
		bulletFieldEdit.setText("0");
		clothingFieldEdit.setText("0");
		gunFieldEdit.setText("0");
		medicineFieldEdit.setText("0");
		foodFieldEdit.setText("0");
		wAxleFieldEdit.setText("0");
		wWheelFieldEdit.setText("0");
		wTongueFieldEdit.setText("0");
		oxenFieldEdit.setText("0");
	}
}